package com.wl.ucenter.controller;


import com.wl.connonutils.JwtUtils;
import com.wl.connonutils.R;
import com.wl.connonutils.ordervo.MemberOrder;
import com.wl.ucenter.entity.Member;
import com.wl.ucenter.entity.vo.RegistVo;
import com.wl.ucenter.service.MemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author wl
 * @since 2021-09-02
 */
@RestController
@RequestMapping("/ucenter/member/")
public class MemberController {

    @Autowired
    private MemberService memberService;

    @PostMapping("login")
    public R login(@RequestBody Member member){
        String token = memberService.login(member);
        return R.ok().data("token",token);
    }

    @PostMapping("register")
    public R registerUser(@RequestBody RegistVo registVo){
        memberService.register(registVo);
        return R.ok();
    }

    //根据token获取用户信息
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库根据用户id获取用户信息
        Member member = memberService.getById(memberId);
        return R.ok().data("userInfo",member);
    }

    @GetMapping("getMemberInfoById/{id}")
    public MemberOrder getMemberInfoById(@PathVariable("id") String id){
        MemberOrder memberOrder = new MemberOrder();
        Member byId = memberService.getById(id);
        BeanUtils.copyProperties(byId,memberOrder);
        return memberOrder;
    }

    //查询某一天的注册人数
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable("day") String day){
        Integer count = memberService.coutRegister(day);
        return R.ok().data("count",count);
    }
}

